using System;
using System.Collections.Generic;
using KKCai.HNKL.Model;
using KKCai.HNKL.Utility;

namespace KKCai.HNKL.DAL
{
    public class HNKLDao : Core.AbstractDao<HNKLInfo>
    {
    }

    public class A1Dao : OmitDao<A1Info>
    {
        protected override IList<int> GetIds(string value)
        {
            const string hql = "select r.id from HNKLInfo r where A1 = ?";

            return Session.CreateQuery(hql)
                .SetString(0, value)
                .List<int>();
        }

        protected override IList<A1Info> GetWinCode(HNKLInfo record)
        {
            const string hql = "from A1Info where Value = ?";

            return Session.CreateQuery(hql)
                .SetString(0, record.A1)
                .List<A1Info>();
        }
    }

    public class A2Dao : OmitDao<A2Info>
    {
        protected override IList<int> GetIds(string value)
        {
            const string hql = "select r.id from HNKLInfo r where Result like ?";

            return Session.CreateQuery(hql)
                .SetString(0, MatchMode.Anywhere(value))
                .List<int>();
        }

        protected override IList<A2Info> GetWinCode(HNKLInfo record)
        {
            List<string> array = Utils.GetA2(record.Result);

            string hql = "from A2Info where Value in (:val)";

            return Session.CreateQuery(hql)
                .SetParameterList("val", array)
                .List<A2Info>();
        }
    }

    public class A3Dao : OmitDao<A3Info>
    {
        protected override IList<int> GetIds(string value)
        {
            const string hql = "select r.id from HNKLInfo r where A3 = ?";

            return Session.CreateQuery(hql)
                .SetString(0, value)
                .List<int>();
        }

        protected override IList<A3Info> GetWinCode(HNKLInfo record)
        {
            const string hql = "from A3Info where Value = ?";

            return Session.CreateQuery(hql)
                .SetString(0, record.A3)
                .List<A3Info>();
        }
    }

    public class P2Dao : OmitDao<P2Info>
    {
        protected override IList<int> GetIds(string value)
        {
            string[] array = value.Split(' ');
            string value2 = String.Format("{0} {1}", array[1], array[0]);

            const string hql = "select r.id from HNKLInfo r where Result like ? or Result like ?";

            return Session.CreateQuery(hql)
                .SetString(0, MatchMode.Anywhere(value))
                .SetString(1, MatchMode.Anywhere(value2))
                .List<int>();
        }

        protected override IList<P2Info> GetWinCode(HNKLInfo record)
        {
            List<string> array = Utils.GetP2(record.Result);

            string hql = "from P2Info where Value in (:val)";

            return Session.CreateQuery(hql)
                .SetParameterList("val", array)
                .List<P2Info>();
        }
    }

    public class P3Dao : OmitDao<P3Info>
    {
        protected override IList<int> GetIds(string value)
        {
            const string hql = "select r.id from HNKLInfo r where P3 = ?";

            return Session.CreateQuery(hql)
                .SetString(0, value)
                .List<int>();
        }

        protected override IList<P3Info> GetWinCode(HNKLInfo record)
        {
            return Session.CreateQuery("from P3Info where Value = ?")
                .SetString(0, record.P3)
                .List<P3Info>();
        }
    }

    public class N2Dao : OmitDao<N2Info>
    {
        protected override IList<N2Info> GetWinCode(HNKLInfo record)
        {
            List<string> array = Utils.Get8P2(record.ASC_Result);

            const string hql = "from N2Info where Value in (:val)";

            return Session.CreateQuery(hql)
                .SetParameterList("val", array)
                .List<N2Info>();
        }
    }

    public class N3Dao : OmitDao<N3Info> 
    {
        protected override IList<N3Info> GetWinCode(HNKLInfo record)
        {
            List<string> array = Utils.Get8P3(record.ASC_Result);

            const string hql = "from N3Info where Value in (:val)";

            return Session.CreateQuery(hql)
                .SetParameterList("val", array)
                .List<N3Info>();
        }
    }

    public class N4Dao : OmitDao<N4Info> 
    {
        protected override IList<N4Info> GetWinCode(HNKLInfo record)
        {
            List<string> array = Utils.Get8P4(record.ASC_Result);

            const string hql = "from N4Info where Value in (:val)";

            return Session.CreateQuery(hql)
                .SetParameterList("val", array)
                .List<N4Info>();
        }
    }

    public class N5Dao : OmitDao<N5Info> 
    {
        protected override IList<N5Info> GetWinCode(HNKLInfo record)
        {
            List<string> array = Utils.Get8P5(record.ASC_Result);

            const string hql = "from N5Info where Value in (:val)";

            return Session.CreateQuery(hql)
                .SetParameterList("val", array)
                .List<N5Info>();
        }
    }
}
